using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// Author: XiaoChunhu
    /// DateTime: 2008-11-4
    /// insert data or update data
    /// </summary>
    public partial class StationCodeEntry : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140820";//set the Menu
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);//add event to chanage the language
        }

        #endregion

        #region page load and set the page lanuage

        //when the page load,these funtion will be run
        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;//allow to chanage the page
            if (!IsPostBack)
            {
                SetLanguage();
                SetPageLoad();
                this.btnNew.Visible = false;
                this.txtSession.Visible = false;
            }

        }

        //when you chanage the language,this function will be run
        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
            if (IsPostBack)  //added by Huangyao 20090119: fixed TD 1127
            {
                SetPageLoad();
            }
        }

        //get the word for the page and by the lanuage
        private void SetLanguage()
        {
            this.lblTitle.Text = this.GetLabel("SOM_ShiftCodeEntryTitle");
            this.lblDescription.Text = this.GetLabel("SOM_Description", true);
            this.lblStation.Text = this.GetLabel("SOM_Station", true);
            this.lblVoid.Text = this.GetLabel("SOM_Void", true);
            this.btnSave.Text = this.GetLabel("COM_Save");
            this.btnNew.Text = this.GetLabel("SOM_ShiftCodeEntryAddNew");
            this.btnSaveReturn.Text = this.GetLabel("COM_SaveAndReturn");
            this.btnSaveNew.Text = this.GetLabel("SOM_SaveNew");
            this.btnCancel.Text = this.GetLabel("COM_Cancel");
            // add by dylan 20150708 for u53
            this.lblControlStation.Text = this.GetLabel("SOM_ControlStation", true);
            this.lblEMTSStationCode.Text = this.GetLabel("SOM_ETMSStationCode", true);
            BindStation();
            BindControlStation();
            txtDescription.Enabled = false;
        }

        /// <summary>
        /// at the pageload this function will run, and if nothing send here,
        /// it will load a new page and if something send here it will bind some data on the control by the CleanShiftID
        /// </summary>
        private void SetPageLoad()
        {
            if (this.Request.QueryString["StationID"] != null)//is something send here 
            {
                txtSession.Text = this.Request.QueryString["StationID"].Trim();
                //get datatable by the StationID
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.GetStationByKey(this.LangCode, int.Parse(this.Request.QueryString["StationID"]));
                if (bool.Parse(dt.Rows[0]["FlagObsolete"].ToString()))
                {
                    ddlStation.Text = dt.Rows[0]["LocationID"].ToString();
                    txtDescription.Text = dt.Rows[0]["Description"].ToString();//bind the Description on the control
                    chVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());//bind the FlagObsolete on the control

                    /****************** add by dylan 20150707 for u53 start*****************************/
                    this.ddlControlStation.SelectedValue = dt.Rows[0]["CtrlStationID"].ToString();
                    this.txtEMTSStationCode.Text = dt.Rows[0]["ETMStationCode"].ToString();
                    ddlControlStation.Enabled = false;
                    this.txtEMTSStationCode.Enabled = false;
                    /****************** add by dylan 20150707 for u53   end*****************************/

                    ddlStation.Enabled = false;
                    txtDescription.Enabled = false;
                    chVoid.Enabled = false;
                    btnNew.Enabled = false;
                    btnSave.Enabled = false;
                    btnSaveNew.Enabled = false;
                    btnSaveReturn.Enabled = false;
                }
                else
                {
                    ddlStation.Text = dt.Rows[0]["LocationID"].ToString();
                    /****************** add by dylan 20150707 for u53 start*****************************/
                    if (dt.Rows[0]["LocationTypeID"].ToString() == "1")
                    {
                        this.lblEm.Visible = true;
                        this.HiddenFieldLocationTypeId.Value = "1";
                    }
                    else
                    {
                        this.lblEm.Visible = false;
                        this.HiddenFieldLocationTypeId.Value = "0";
                    }
                    this.ddlControlStation.SelectedValue = dt.Rows[0]["CtrlStationID"].ToString();
                    this.txtEMTSStationCode.Text = dt.Rows[0]["ETMStationCode"].ToString();
                    /****************** add by dylan 20150707 for u53   end*****************************/
                    txtDescription.Text = dt.Rows[0]["Description"].ToString();//bind the Description on the control
                    chVoid.Checked = bool.Parse(dt.Rows[0]["FlagObsolete"].ToString());//bind the FlagObsolete on the control
                    ddlStation.Enabled = false;
                }
            }
            //add the message on the button,when you click the button,it will to check the control and show the message
            this.btnSave.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_StationCode") + "')");
            this.btnSaveNew.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_StationCode") + "')");
            this.btnSaveReturn.Attributes.Add("onclick", "return CheckData('" + this.GetMessage("SOM_StationCode") + "')");
        }

        #endregion

        #region Bind Some Data

        //use this function can to bind ddlStation
        private void BindStation()
        {
            //get the location code by the LangCode
            DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.GetAllLocation(0, this.LangCode);//Modified by jensen 2009-02-10
            BindListControl(this.ddlStation, "LocationID", "LocationCode", dt, true, "");//bind data for the control
            dt.Dispose();
            dt = null;
        }

        /// <summary>
        /// Get all Odm location control station
        /// add by dylan 20150707 for u53
        /// </summary>
        private void BindControlStation()
        {
            //get the location code by the LangCode
            DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.GetAllControlStation(this.LangCode);
            BindListControl(this.ddlControlStation, "LocationID", "ControlStationCode", dt, true, "");
            dt.Dispose();
            dt = null;
        }

        //use this function can do insert or update
        private void Save()
        {
            try
            {
                // add by dylan 20150708 for u53
                int controlStation = !string.IsNullOrEmpty(ddlControlStation.SelectedValue) ? int.Parse(ddlControlStation.SelectedValue) : 0;

                //update to table,when there is something be sent here,click the save will to update the table
                MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.UpdateStation(
                    int.Parse(ddlStation.SelectedValue),
                    controlStation,
                    this.txtEMTSStationCode.Text.Trim(),
                    chVoid.Checked,
                    this.ODMUser.ID,//get the user id at now
                    MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime(),
                    this.ODMUser.ID);
                this.Alert(this.GetMessage("SOM_CleanShiftSuccessful"));
            }
            catch
            {
                this.Alert(this.GetMessage("SOM_CleanShiftFailing"));
            }
        }

        //this function will to check you wang to insert data exist
        //private bool CheckUnique(bool insert)
        //{
        //    //get the Count,if the Count more than 0, you wang to insert data is exist,you can't to do insert
        //    int DataCount = MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.GetInsert(int.Parse(ddlStation.SelectedValue)).Rows.Count;
        //    if (DataCount > 0)
        //    {
        //        this.Alert(string.Format(this.GetMessage("SOM_HaveSomeData"), this.ddlStation.SelectedValue, this.txtDescription.Text));
        //        return false;
        //    }
        //    return true;
        //}

        /// <summary>
        /// clear the page,if there are some data on the page,then these data will lost
        /// </summary>
        private void Clear()
        {
            //Bind the new data
            BindStation();
            // add by dylan 20150708 for u53
            BindControlStation();
            this.txtEMTSStationCode.Text = string.Empty;
            txtDescription.Text = "";
            this.chVoid.Checked = false;
            //set the Enabled,if there are some control's Enabled is false,then these Enabled will chanage to true
            this.chVoid.Enabled = true;
            this.ddlStation.Enabled = true;
        }

        private bool CheckData()
        {
            //check station: must exists in table SOM_CDStation
            //update by gordon 20151019  U247
            //if (!DAL.SOM.Event.StationEventDa.IsLegalStation(this.ODMUser.StationID))
            if (!DAL.SOM.Event.StationEventDa.IsLegalStation(this.ODMUser.LocationID))
            {
                //this.Alert("Current station is not an legal station for SOM");
                this.Alert(this.GetMessage("SOM_LegalStationCheck"));
                return false;
            }
            // add by dylan 20150708 for u53
            if (this.HiddenFieldLocationTypeId.Value.Equals("1") && string.IsNullOrEmpty(this.ddlControlStation.SelectedValue))
            {
                this.Alert(GetMessage("SOM_ControlStationBeNull"));
                return false;
            }
            return true;
        }

        #endregion

        #region some event

        /// <summary>
        /// //when you want to create a new data,and there are some data on the page,you can click the "new"
        /// </summary>
        protected void btnNew_Click(object sender, EventArgs e)
        {
            Clear();//use the Clear function to have a new page
        }

        /// <summary>
        /// //when you write some data on the page,and you want to save these data,you can click the "Save"
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            //if (txtSession.Text != "")
            //{
            //    Save(int.Parse(txtSession.Text), 2);
            //}
            //else
            //{
            //    //to use a function to do the check the data exist
            //    if (CheckUnique(true))
            //    {
            //        Save(0, 1);
            //    }
            //}
            Save();
        }

        //when you write some data on the page,and you want to save these data and return the last page,you can click the "Save And Return"
        protected void btnSaveReturn_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            ////is there something be sent here,if have,it will do update,if no, it will do insert
            //if (txtSession.Text != "")
            //{
            //    Save(int.Parse(txtSession.Text), 2);
            //}
            //else
            //{
            //    //to use a function to do the check the data exist
            //    if (CheckUnique(true))
            //    {
            //        Save(0, 1);
            //    }
            //}
            Save();
            //to return the last page
            string script = "<script language=javascript>function btnChange_Click(){window.location.href='StationCodeList.aspx'} window.setTimeout(btnChange_Click,0);" + "</script>";
            this.Page.RegisterStartupScript("aaa", script);
        }

        //when you write some data on the page,and you want to save these data and you have other data want to insert,you can click the "Save And New"
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            ////is there something be sent here,if have,it will do update,if no, it will do insert
            //if (txtSession.Text!="")
            //{
            //    Save(int.Parse(txtSession.Text), 2);
            //    txtSession.Text = "";
            //}
            //else
            //{
            //    //to use a function to do the check the data exist
            //    if (CheckUnique(true))
            //    {
            //        Save(0, 1);
            //    }
            //}
            Save();
            //to clear the data on the page to insert other data
            Clear();
        }

        //if you want to do nothing,and want to return the last page,you can click the "Cancel"
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //return the last page
            this.Response.Redirect("StationCodeList.aspx");
        }

        //if you chanage the ddlStation, this event will be run
        protected void ddlStation_TextChanged(object sender, EventArgs e)
        {
            //added by huangyao 20090120: fixed TD 1131
            int stationID = 0;
            int.TryParse(ddlStation.SelectedValue, out stationID);

            // modify by dylan fix Found a bug
            if (stationID == 0) 
            {
                this.lblEm.Visible = false;
                txtDescription.Text = "";
                txtEMTSStationCode.Text = "";
                ddlControlStation.SelectedValue = "";
                this.HiddenFieldLocationTypeId.Value = "0";
                return;
            }
            //--end : added by huangyao 20090120: fixed TD 1131
            //get the datatable by ID
            DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.GetDescriptionByID(this.LangCode, stationID);
            if (dt != null && dt.Rows.Count > 0)
            {
                txtDescription.Text = dt.Rows[0]["Description"].ToString();//bind Description on the control
            }
            else
            {
                txtDescription.Text = "";
            }
            dt.Dispose();
            dt = null;

            /**********************add by dylan 20150707 for u53 start*************************/
            int locationTypeId = MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.GetLocationTypeId(stationID);
            if (locationTypeId == 1)
            {
                this.lblEm.Visible = true;
                this.HiddenFieldLocationTypeId.Value = "1";
            }
            else
            {
                this.HiddenFieldLocationTypeId.Value = "0";
                this.lblEm.Visible = false;
            }
            DataTable dt1 = MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.GetControlStationByLocationId(this.LangCode, stationID);
            if (dt1 != null && dt1.Rows.Count > 0)
            {
                ddlControlStation.SelectedValue = dt1.Rows[0]["LocationID"].ToString();
            }
            else
            {
                ddlControlStation.SelectedValue = "";
            }
            dt1.Dispose();
            dt1 = null;

            DataTable dt2 = MTRC.ODMS.DAL.SOM.CodeSetup.CDStation.GetETMSStationCodeByLocationId(stationID);
            if (dt2 != null && dt2.Rows.Count > 0)
            {
                txtEMTSStationCode.Text = dt2.Rows[0]["Code"].ToString();
            }
            else
            {
                txtEMTSStationCode.Text = "";
            }
            dt2.Dispose();
            dt2 = null;
            /**********************add by dylan 20150707 for u53  end *************************/

        }

        protected void chVoid_CheckedChanged(object sender, EventArgs e)
        {
            if (chVoid.Checked)
            {
                Alert(this.GetMessage("SOM_MsgVoid"));
            }
        }

        #endregion
    }
}
